package gui;
/*
 * This file is part of VLCJ.
 *
 * VLCJ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VLCJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VLCJ.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
 * 
 * Copyright 2009, 2010, 2011, 2012 Caprica Software Limited.
 */

import java.io.File;

import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.JProgressBar;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;

import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.MediaPlayer;
import uk.co.caprica.vlcj.player.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import java.awt.FlowLayout;
import javax.swing.JLabel;

@SuppressWarnings("serial")
public class VReproductor extends JPanel{

	private String url;
	private JButton play;
	private JButton pause;
	private JButton stop;
	private JSlider volumen;
	private JProgressBar progreso;
	private JPanel controles;
	private EmbeddedMediaPlayerComponent reproductor;
	private JLabel tActual;
	private JLabel label = new JLabel("/");
	private JLabel tTotal;
  
	public static void main(String[] args) {
    
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Throwable e) {
			e.printStackTrace();
		}
	  
		JFileChooser j = new JFileChooser();
		if(j.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) return;
		final String mrl = j.getSelectedFile().toString();
    
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				javax.swing.JFrame jf = new javax.swing.JFrame();
				VReproductor vr = new VReproductor(mrl);
				jf.getContentPane().add(vr);
				jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				jf.pack();
				jf.setVisible(true);
				vr.start();
			}
		});
	}

	public VReproductor(final String mrl) {
	  
		String dir = System.getProperty("user.dir",".");
		dir = dir.substring(0, dir.lastIndexOf(File.separatorChar));
		dir = dir + File.separatorChar + "proofing" + File.separatorChar + "resources" 
		    + File.separatorChar + "lib" + File.separatorChar + "VideoLAN" + File.separatorChar + "VLC";
		/*
		NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(),dir);
		Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(),LibVlc.class); 
		*/
		// TODO URL
		url = mrl;
		url = "C:\\USERS\\KILO\\DESKTOP\\Los Simpsons - Pitonisa.mpg";
		setPreferredSize(new Dimension(640, 480));
		setLayout(new BorderLayout());
	  
		/*// TODO window builder
		
		reproductor = new EmbeddedMediaPlayerComponent(){
			{	
				getMediaPlayer().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
					@Override
					public void positionChanged(MediaPlayer mediaPlayer, final float newPosition) {
						SwingUtilities.invokeLater(new Runnable(){
							@Override
							public void run() {
								int value = (int) Math.min(100, Math.ceil(newPosition * 100.0f));
								progreso.setValue(value);
							}
						});
					}
					
					@Override
				    public void playing(final MediaPlayer mediaPlayer) {
						pause.setVisible(true);
						play.setVisible(false);
						progreso.setEnabled(true);
						controles.requestFocus();
						
						SwingUtilities.invokeLater(new Runnable(){
							@Override
							public void run() {
								
								long t = mediaPlayer.getLength();
								int hours = (int) Math.ceil ( t / 3600000 );
								int minutes = (int) Math.ceil (( t % 3600000) / 60000);
								int seconds = (int) Math.ceil ((( t % 360000) % 60000) / 1000);
								
								tTotal.setText(((hours>0)?hours+":":"")+((minutes>9)?"":"0")+minutes+((seconds>9)?":":":0")+seconds);
							}
						});
					
				    }
				    @Override
				    public void paused(MediaPlayer mediaPlayer) {
				    	play.setVisible(true);
				    	pause.setVisible(false);
				    	controles.requestFocus();
				    }
				    @Override
				    public void stopped(MediaPlayer mediaPlayer) {
				    	play.setVisible(true);
				    	pause.setVisible(false);
						progreso.setValue(0);
						progreso.setEnabled(false);
						controles.requestFocus();
						tActual.setText("00:00");
				    }
				    @Override
				    public void finished(MediaPlayer mediaPlayer) {
				    	play.setVisible(true);
				    	pause.setVisible(false);
						progreso.setValue(0);
						progreso.setEnabled(false);
						controles.requestFocus();
						tActual.setText("00:00");
				    }
				    @Override
				    public void timeChanged(final MediaPlayer mediaPlayer, final long newTime) {
				    	SwingUtilities.invokeLater(new Runnable(){
							@Override
							public void run() {
								
								int hours = (int) Math.ceil ( newTime / 3600000 );
								int minutes = (int) Math.ceil (( newTime % 3600000) / 60000);
								int seconds = (int) Math.ceil ((( newTime % 360000) % 60000) / 1000);
								
								tActual.setText(((hours>0)?hours+":":"")+((minutes>9)?"":"0")+minutes+((seconds>9)?":":":0")+seconds);
							}	
				    	});
				    }
				});
			}
		};
		add(reproductor,BorderLayout.CENTER);
		//*////
    
		controles = new JPanel();
		FlowLayout flowLayout = (FlowLayout) controles.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		this.add(controles,BorderLayout.SOUTH);
    
		play = new JButton(new ImageIcon(getClass().getResource("/img/play.png")));
		play.setBorder(null);
		play.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if(reproductor.getMediaPlayer().isPlayable())
					reproductor.getMediaPlayer().play();
				 else
					reproductor.getMediaPlayer().playMedia(url);
			}
		});
		controles.add(play);
    
		pause = new JButton(new ImageIcon(getClass().getResource("/img/pause.png")));
		pause.setBorder(null);
		pause.setVisible(false);
		pause.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				reproductor.getMediaPlayer().pause();
			}
		});
		controles.add(pause);

		stop = new JButton(new ImageIcon(getClass().getResource("/img/stop.png")));
		stop.setBorder(null);
		stop.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				reproductor.getMediaPlayer().stop();
			}
		});
		controles.add(stop);
    
		volumen = new JSlider();
		volumen.setPreferredSize(new Dimension(50, 23));
		volumen.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				SwingUtilities.invokeLater(new Runnable(){
					@Override
					public void run() {
						reproductor.getMediaPlayer().setVolume(volumen.getValue()*2);
					}});
			}
		});
		controles.add(volumen);
		
		progreso = new JProgressBar();
		progreso.setPreferredSize(new Dimension(425, 14));
		progreso.addMouseListener(new MouseListener(){	
			@Override
			public void mouseClicked(final MouseEvent e) {
				if(!progreso.isEnabled()) return;
				SwingUtilities.invokeLater(new Runnable(){
					@Override
					public void run() {
						float f = ((float)e.getX())/((JProgressBar)e.getSource()).getWidth();
						progreso.setValue(Math.round(100*f));
						reproductor.getMediaPlayer().setPosition(f);
					}
				});
			}
			@Override
			public void mouseEntered(MouseEvent e) {}
			@Override
			public void mouseExited(MouseEvent e) {}
			@Override
			public void mousePressed(MouseEvent e) {}
			@Override
			public void mouseReleased(MouseEvent e) {}
		});
		controles.add(progreso);  
		
		tActual = new JLabel();
		tTotal = new JLabel();
		
		tActual.setText("00:00");
		tTotal.setText("00:00");
		
		controles.add(tActual);
		controles.add(label);
		controles.add(tTotal);
		/*
		addHierarchyListener(new HierarchyListener() {
            @Override
            public void hierarchyChanged(HierarchyEvent e) {
                if ((e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0)
                    if (e.getComponent().isDisplayable());
                    else
                    	reproductor.getMediaPlayer().release();
            }
        });	
		*/
	}
	
	public void start(){
		this.reproductor.getMediaPlayer().playMedia(url);
	}
}